/**
 * 
 */
package net.looker.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;

import net.looker.data.Category;
import net.looker.util.URLUtils;

/**
 * 一个站点配置
 * @author solosky
 *
 */
public class SiteConfig extends Config
{
	/**
	 * 栏目列表
	 */
	private ArrayList <Category> categoryList;
	
	/**
	 * 构造函数
	 *@param File Xml 一个站点的配置文件
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws JDOMException 
	 */
	public SiteConfig(File xmlFile) throws FileNotFoundException, IOException, JDOMException
	{
		categoryList = new ArrayList<Category>();
		load(xmlFile);
	}
	
	/**
	 * 建立栏目列表
	 */
	public void buildCategoryList()
	{
		//站点配置中栏目列表迭代器
		Iterator<Element> it = xmlDoc.getRootElement().getChild("categorys").getChildren().iterator();
		while (it.hasNext()){
			//添加至栏目列表
			categoryList.add(new Category( new CategoryConfig(new Document((Element) it.next().clone())), this));
		}
		
	}
	/**
	 * 返回栏目配置列表
	 */
	public ArrayList<Category> getCategoryList()
	{
		return categoryList;
	}
	
	/**
	 * 返回栏目HASH
	 */
	public String getSiteHash()
	{
		return URLUtils.hash2(get("site.url"));
	}
	
	/**
	 * 
	 * @param args
	 * @throws SecurityException
	 * @throws IOException
	 * @throws JDOMException
	 */
	
	public static void main(String []args) throws SecurityException, IOException, JDOMException
	{
		Logger lg = Logger.getLogger("looker.global");
		FileHandler fh =new FileHandler("./log/global.txt");
		fh.setFormatter(new SimpleFormatter());
		lg.addHandler(fh);
		SiteConfig sc = new SiteConfig(new File("./config/site/www.cdut.edu.cn.xml"));
		lg.info("成功读取配置文件");
	System.out.println(sc.get("list.index"));
	}
}
